﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Smugmug.Tests.CaptureRequests
{
    public static class Request
    {
        public static readonly string ValueWildCard = "*";
        public static readonly string KeyWildCard = "!";

        public static IEnumerable<string> StandardKeyWildcards
        {
            get
            {
                yield return "Callback";
                yield return "Pretty";
                yield return "Strict";
            }
        }

        public static WebRequest CaptureRequest(Action action, SmugmugClient client, IEnumerable<string> keyWildcards, IEnumerable<string> valueWildcards)
        {
#if DEBUG
            try
            {
                action();
                return new WebRequest
                {
                    Method = client.LastMethod,
                    Response = client.LastResponse,
                    Url = ProcessWildcards(client.LastRequest, keyWildcards, valueWildcards)
                };
            }
            catch (SmugmugException e)
            {
                return new WebRequest
                {
                    Method = client.LastMethod,
                    Response = client.LastResponse,
                    Url = ProcessWildcards(client.LastRequest, keyWildcards, valueWildcards),
                    Error = e.Error.ToString(),
                    ErrorCode = (int)e.Error
                };
            }
#else
            return new WebRequest();
#endif
        }

        private static string ProcessWildcards(Uri url, IEnumerable<string> keyWildcards, IEnumerable<string> valueWildcards)
        {
            if ((keyWildcards == null || keyWildcards.Count() == 0) && (valueWildcards == null || valueWildcards.Count() == 0))
                return url.ToString();

            QueryStringHelper query = new QueryStringHelper(url.Query);
            QueryStringHelper newQuery = new QueryStringHelper();
            foreach (var pair in query)
            {
                string newKey = pair.Key;
                string newValue = pair.Value;

                if (keyWildcards != null && keyWildcards.Contains(pair.Key))
                {
                    newKey += KeyWildCard;
                }

                if (valueWildcards != null && valueWildcards.Contains(pair.Key))
                {
                    newValue = ValueWildCard;
                }

                newQuery[newKey] = newValue;
            }

            foreach (string key in keyWildcards)
            {
                string compositeKey = key + KeyWildCard;
                if (!newQuery.Keys.Contains(compositeKey))
                {
                    newQuery[compositeKey] = ValueWildCard;
                }
            }

            return String.Format("{0}://{1}{2}{3}", url.Scheme, url.Host, url.AbsolutePath, newQuery);
        }
    }
}
